package com.elvin.zoe.pojo.structure;

import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

import com.elvin.zoe.pojo.CallMessage;
import com.elvin.zoe.pojo.ImMessage;
import com.elvin.zoe.pojo.Message;

public class MessageGroup {

    protected Date messageGroupDate;
    protected MessageCount messageCount;
    private Set<Message> messages;
    private Set<MessageGroup> messageGroups;

    public MessageGroup(Date messageGroupDate) {
        super();
        this.messageGroupDate = messageGroupDate;
    }

    public void add(Message message){
        if(messages == null){
            messages = new TreeSet<Message>(new Comparator<Message>() {
                @Override
                public int compare(Message o1, Message o2) {

					if (o1 instanceof ImMessage && o2 instanceof ImMessage) {
						ImMessage imMessage1 = (ImMessage) o1;
						ImMessage imMessage2 = (ImMessage) o2;
						long timedifference = imMessage1.getDateTime().getTime() - imMessage2.getDateTime().getTime();
						if (Math.abs(timedifference) > 1000 * 60 * 5L)
							return imMessage1.getDateTime().compareTo(imMessage2.getDateTime());
						else
							return imMessage1.getIndex() - imMessage2.getIndex();
					}

                    Date datetime1 = o1 instanceof CallMessage ? ((CallMessage)o1).getEndDatetime() : o1.getDateTime();
                    Date datetime2 = o2 instanceof CallMessage ? ((CallMessage)o2).getEndDatetime() : o2.getDateTime();
                    return datetime1.compareTo(datetime2);
                }
            });
        }
        messages.add(message);

        if(messageCount == null){
            messageCount = new MessageCount();
        }
        messageCount.addCount(message);
    }
    public Collection<Message> getMessages() {
        return messages;
    }

    public void add(MessageGroup messageGroup){
        if(messageGroups == null){
            messageGroups = new TreeSet<MessageGroup>(new Comparator<MessageGroup>() {
                @Override
                public int compare(MessageGroup o1, MessageGroup o2) {
                    return o1.getMessageGroupDate().compareTo(o2.getMessageGroupDate());
                }
            });
        }
        messageGroups.add(messageGroup);

        if(messageCount == null){
            messageCount = new MessageCount();
        }
        messageCount.addCount(messageGroup.getMessageCount());
    }
    public Collection<MessageGroup> getMessageGroups() {
        return messageGroups;
    }

    public Date getMessageGroupDate() {
        return messageGroupDate;
    }
    public void setMessageGroupDate(Date messageGroupDate) {
        this.messageGroupDate = messageGroupDate;
    }
    public MessageCount getMessageCount() {
        return messageCount;
    }
    public void setMessageCount(MessageCount messageCount) {
        this.messageCount = messageCount;
    }

}
